package com.rms.server.Service;

import com.rms.server.DTO.DishWithIngredientsDTO;
import com.rms.server.Dao.DishIngredientAssociationMapper;
import com.rms.server.Dao.DishMapper;
import com.rms.server.Dao.IngredientMapper;
import com.rms.server.Domain.DishIngredientAssociation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DishIngredientAssociationService {
    @Autowired
    private DishIngredientAssociationMapper dishIngredientAssociationMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private IngredientMapper ingredientMapper;

    // 添加菜品原料关联记录
    public String addDishIngredientAssociation(Long dishId, Long ingredientId, double quantityUsed) {
        // 校验菜品是否存在
        if (dishMapper.selectDishById(dishId) == null) {
            return "Dish ID 不存在";
        }
        // 校验原料是否存在
        if (ingredientMapper.selectIngredientById(ingredientId) == null) {
            return "Ingredient ID 不存在";
        }
        // 创建关联对象并插入
        DishIngredientAssociation association = new DishIngredientAssociation();
        association.setDishId(dishId);
        association.setIngredientId(ingredientId);
        association.setQuantityUsed(quantityUsed);
        int result = dishIngredientAssociationMapper.insertDishIngredientAssociation(association);
        return result > 0 ? "新增关联记录成功" : "新增关联记录失败";
    }

// 获取所有菜品及其原料信息
    public List<DishWithIngredientsDTO> getAllDishesWithIngredients() {
        // 查询所有菜品及其原料数据
        List<DishWithIngredientsDTO> dishList = dishIngredientAssociationMapper.findAllDishesWithIngredients();

        // 将相同 dishId 的原料信息合并到同一个 DishWithIngredientsDTO 对象中
        Map<Long, DishWithIngredientsDTO> dishMap = dishList.stream()
                .collect(Collectors.toMap(
                        DishWithIngredientsDTO::getDishId,
                        dish -> {
                            DishWithIngredientsDTO dto = new DishWithIngredientsDTO();
                            dto.setDishId(dish.getDishId());
                            dto.setDishName(dish.getDishName());
                            dto.setIngredients(dish.getIngredients());
                            return dto;
                        },
                        (existing, replacement) -> {
                            existing.getIngredients().addAll(replacement.getIngredients());
                            return existing;
                        }
                ));

        return new ArrayList<>(dishMap.values());
    }

    //根据id获取菜品及其原料信息
    public DishWithIngredientsDTO getDishWithIngredientsById(Long dishId) {
        // 查询所有菜品及其原料数据
        List<DishWithIngredientsDTO> dishList = dishIngredientAssociationMapper.findAllDishesWithIngredients();

        // 查找指定 dishId 的菜品并返回
        return dishList.stream()
                .filter(dish -> dish.getDishId().equals(dishId))
                .findFirst()
                .orElse(null); // 如果没有找到，返回 null
    }


}
